package user

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/user"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserSearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserSearchLogic {
	return &UserSearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserSearchLogic) buildRpcReq(req *types.UserSearchRequest) *userservice.UserSearchRequest {
	rpcReq := &userservice.UserSearchRequest{
		Id:                req.Id,
		Account:           req.Account,
		RegisterIp:        req.RegisterIp,
		Remark:            req.Remark,
		ChannelCode:       req.ChannelCode,
		RegisterDevice:    req.RegisterDevice,
		Status:            req.Status,
		AgentLevel:        req.AgentLevel,
		InviteCode:        req.InviteCode,
		AmountSort:        req.AmountSort,
		VipLevel:          req.VipLevel,
		RegisterTimeStart: req.RegisterTimeStart,
		RegisterTimeEnd:   req.RegisterTimeEnd,
		OnlineTimeStart:   req.OnlineTimeStart,
		OnlineTimeEnd:     req.OnlineTimeEnd,
		GroupId:           req.GroupId,
		SafeAmountSort:    req.SafeAmountSort,
		PageSize:          req.PageSize,
		Page:              req.Page,
	}
	return rpcReq
}

func (l *UserSearchLogic) UserSearch(req *types.UserSearchRequest) (resp *types.UserSearchResponse, err error) {
	rpcReq := l.buildRpcReq(req)
	searchResp, err := l.svcCtx.UserServiceRPC.UserSearch(l.ctx, rpcReq)
	if err != nil {
		logc.Error(l.ctx, "UserSearch rpc call error: %v", err)
		return &types.UserSearchResponse{
			Pagination:     types.Pagination{},
			UserList:       []*types.UserSearchItem{},
			UserWalletList: []*types.UserWalletSearchItem{},
		}, nil
	}

	var userIds []int64
	userList := make([]*types.UserSearchItem, len(searchResp.UserList))
	for index, item := range searchResp.UserList {
		userList[index] = &types.UserSearchItem{
			Id:               item.Id,
			Account:          item.Account,
			Phone:            item.Phone,
			Email:            item.Email,
			Password:         item.Password,
			Nickname:         item.Nickname,
			Sex:              item.Sex,
			AvatarUrl:        item.AvatarUrl,
			InviteCode:       item.InviteCode,
			ChannelCode:      item.ChannelCode,
			RegisterIp:       item.RegisterIp,
			RegisterDevice:   item.RegisterDevice,
			RegisterTime:     item.RegisterTime,
			FirstLoginTime:   item.FirstLoginTime,
			LastOnlineTime:   item.LastOnlineTime,
			GroupId:          item.GroupId,
			IsBlacklist:      item.IsBlacklist == 1,
			IsOnline:         item.IsOnline,
			Status:           item.Status,
			AgentLevel:       item.AgentLevel,
			VipLevel:         item.VipLevel,
			Remark:           item.Remark,
			Exp:              item.Exp,
			IsPayBack:        item.IsPayBack,
			Balance:          item.Balance,
			RechargeAmount:   item.RechargeAmount,
			RechargeCount:    item.RechargeCount,
			SafeBalance:      item.SafeBalance,
			Integral:         item.Integral,
			RedPacketAmount:  item.RedPacketAmount,
			GuaranteedAmount: item.GuaranteedAmount,
			FirstChargeTime:  item.FirstChargeTime,
			LastChargeTime:   item.LastChargeTime,
			IsGoogle:         item.IsGoogle,
			IsBindingGoogle:  item.IsBindingGoogle,
			GoogleCode:       item.GoogleCode,
			GroupName:        item.GroupName,
		}
		userIds = append(userIds, item.Id)
	}
	resp = &types.UserSearchResponse{
		Pagination: types.Pagination{
			Page:      searchResp.Pagination.Page,
			PageSize:  searchResp.Pagination.PageSize,
			Total:     searchResp.Pagination.Total,
			TotalPage: searchResp.Pagination.TotalPage,
		},
		UserList: userList,
	}

	searchWalletResp, err := l.svcCtx.AssetsRpc.User.UserWallet(l.ctx, &user.UserWalletRequest{UserIds: userIds})
	if err != nil {
		logc.Error(l.ctx, "UserWalletSearch rpc call error: %v", err)
		return resp, nil
	}
	userWalletList := make([]*types.UserWalletSearchItem, len(searchWalletResp.DataList))
	for index, item := range searchWalletResp.DataList {
		userWalletList[index] = &types.UserWalletSearchItem{
			UserId:        item.UserId,
			Balance:       item.Balance,
			ValidMark:     item.ValidMark,
			ValidConsume:  item.ValidConsume,
			TotalRecharge: item.TotalRecharge,
			TotalWithdraw: item.TotalWithdraw,
		}
	}
	resp.UserWalletList = userWalletList
	return
}
